<template>
    <div class="login flex-center">
       <div class="login-box">
        <h3 style="margin-bottom: 20px;">INDOOR-MAP 室内地图系统</h3>
        <el-form
            ref="ruleFormRef"
            :model="ruleForm"
            :rules="rules"
            label-width="0px"
            >
            <el-form-item prop="userName">
                <el-input
                    size="large"
                    v-model="ruleForm.userName"
                    placeholder="请输入账户名"
                    :prefix-icon="User"
                />
            </el-form-item>
            <el-form-item prop="password">
                <el-input
                    size="large"
                    v-model="ruleForm.password"
                    type="password"
                    placeholder="请输入密码"
                    :prefix-icon="Lock"
                />
            </el-form-item>
        </el-form>
        <el-button :loading="loading" @click="handleLogin" size="large" type="primary" style="width: 100%;">登录</el-button>
       </div>
    </div>
</template>
  
<script setup lang="ts">
import { ref, reactive, watch, onMounted, onUnmounted } from 'vue';
import { User, Lock} from '@element-plus/icons-vue'; 
import { ElMessage } from 'element-plus';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { login } from '@/api/permission';

const loading = ref(false);
const ruleFormRef = ref(null);
const store = useStore();
const router = useRouter();
const ruleForm = reactive({
    userName: '',
    password: ''
})

const rules = reactive({
    userName: [
        { required: true, message: '请输入账户名', trigger: 'change' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'change' }
    ],
})

function handleLogin(){
    ruleFormRef.value.validate((valid, fields) => {
        if (valid) {
            loading.value = true;
                const params = {
                    username: ruleForm.userName,
                    password: ruleForm.password
                }
                login(params).then(res=>{
                    store.dispatch('permission/setLogin', true);
                    store.dispatch('permission/setToken', res.data);
                    router.push('/index')
                    loading.value = false;
                });

            //console.log('submit!')
        } 
    })
}

</script>

<style lang="scss" scoped>
.login{
    width: 100vw;
    height: 100vh;
    //background-image: linear-gradient(to top, #48c6ef 0%, #6f86d6 100%);
    //background-image: url(../assets/images/login-bg.jpg);
    background-image: linear-gradient(to top, #48c6ef 0%, #6f86d6 100%);
    background-size: cover;
    .login-box{
        color: white;
        padding: 50px;
        background-color: rgba(255,255,255,.2);
        text-align: center;
        border-radius: 5px;
    }
}
</style>

